layui.extend({
  mainSysUserForm: '{/}' + layui.cache['contentPath'] + '/static/fsoft-web/user/mainSysUserForm'
}).use(['form', 'table', 'eleTree', 'fsoft', 'mainSysUserForm'], function () {
  var $ = layui.$
    , form = layui.form
    , table = layui.table
    , eleTree = layui.eleTree
    , fsoft = layui.fsoft
    , mainSysUserForm = layui.mainSysUserForm
    ;
  var treeId = "fsoft-orgTree";
  var tableId = "sysUserTable";
  var searchFormId = "fsoftSearchForm";
  // 节点点击事件
  eleTree.on("nodeClick(" + treeId + ")", function (d) {
    var currentData = d.data.currentData;
    $("#orgId").val(currentData.id);
    active.doSearch();
  });
  //搜索监听
  form.on('submit(sreach)', function (data) {
    active.doSearch(data.field);
    return false;
  });
  /* 工具条 */
  table.on('toolbar(' + tableId + ')', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    switch (obj.event) {
      case 'add':
        active.showContentForm();
        break;
      case 'removeBatch':
        active.removeBatch(checkStatus.data);
        break;
      case 'refpwd':
        active.refpwd(checkStatus.data);
        break;
      case 'enable':
        active.enable(checkStatus.data);
        break;
      case 'disable':
        active.disable(checkStatus.data);
        break;
      case 'lock':
        active.lock(checkStatus.data);
        break;
      case 'unlock':
        active.unlock(checkStatus.data);
        break;
    }
  });
  //行操作监听 
  table.on('tool(' + tableId + ')', function (obj) {
    var data = obj.data;
    if (obj.event === 'detail') {
      active.refpwd([data]);
    } else if (obj.event === 'edit') {
      active.showContentForm(data);
    } else if (obj.event === 'del') {
      active.removeBatch([data]);
    }
  });

  /* 工具条按钮事件监听 */
  var active = {
    doSearch: function (params) {
      if (params == undefined || params == null)
        params = $.extend({}, form.val(searchFormId));
      //用户列表初始化
      table.render({
        elem: '#' + tableId,
        toolbar: '#batchBtnToolBar'
        , where: params
        , method: 'post'
        , url: layui.cache['contentPath'] + '/sys-user/findList'
        , cols: mainSysUserForm.getCols(),
        page: { limit: 20, limits: [20, 30, 50, 80, 100], theme: '#1E9FFF' },
        height: 'full-108'
      });
    }
    , removeBatch: function (data) { //批量删除
      if (data == undefined || data.length == 0) {
        fsoft.errorMsg("请至少选择1条待删除记录！");
        return false;
      }
      var batchs = new Array();
      $.each(data, function (idx, item) {
        batchs.push(item.id);
      });
      var msg = "确认要删除当前记录？";
      if (batchs.length > 1) {
        msg = "确认要删除已选中的（" + batchs.length + "）条记录？";
      }
      fsoft.confirm("询 问", msg, function (index) {
        fsoft.post(layui.cache['contentPath'] + '/sys-user/remove', batchs, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("删除成功！");
            active.doSearch();
          }
        });
        layer.close(index);
      });
    },
    showContentForm: function (data) {
      var title = '新增帐号';
      var path = layui.cache['contentPath'] + '/sys-user/add/' + $('#orgId').val();
      if (data) {
        title = "帐号编辑-" + data.name;
        path = layui.cache['contentPath'] + '/sys-user/edit/' + data.id;
      }
      layer.open({
        type: 2
        , id: 'mainSysuserModifyFrameLayero'
        , title: '帐号详情'
        , area: ['800px', '500px']
        , title: title
        , content: path
        , end: function () {
          active.doSearch();
        }
      });
    },
    refpwd: function (data) {
      if (data == undefined || data.length == 0) {
        fsoft.errorMsg("请至少选择1条帐号记录");
        return false;
      }
      var ids = new Array();
      var names = [];
      $.each(data, function (idx, item) {
        ids.push(item.id);
        names.push(item.loginAcct);
      });

      layer.open({
        type: 2, title: "修改密码-" + names.join(",")
        , area: ['500px', '230px']
        , content: layui.cache['contentPath'] + '/sys-user/resetpwd/' + ids.join(",")
      });
    },
    lock: function (data) { //加锁帐号
      if (data == undefined || data.length == 0) {
        fsoft.errorMsg("请至少选择1条帐号记录");
        return false;
      }
      var ids = new Array();
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要加锁当前帐号？";
      if (ids.length > 1) {
        msg = "确认要加锁已选中的（" + ids.length + "）个帐号？";
      }
      layer.confirm(msg, function (index) {
        fsoft.put(layui.cache['contentPath'] + '/sys-user/lock', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("加锁成功！");
            active.doSearch();
          } else {
            fsoft.errorMsg(rs.msg);
          }
        });
        layer.close(index);
      });
    }
    , unlock: function (data) {
      if (data == undefined || data.length == 0) {
        fsoft.errorMsg("请至少选择1条帐号记录");
        return false;
      }
      var ids = new Array();
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要解锁当前帐号？";
      if (ids.length > 1) {
        msg = "确认要解锁已选中的（" + ids.length + "）个帐号？";
      }
      layer.confirm(msg, function (index) {
        fsoft.put(layui.cache['contentPath'] + '/sys-user/unlock', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("解锁成功！");
            active.doSearch();
          } else {
            fsoft.errorMsg(rs.msg);
          }
        });
        layer.close(index);
      });
    },
    enable: function (data) {/*启用*/
      if (data == undefined || data.length == 0) {
        fsoft.errorMsg("请至少选择1条要启用的帐号记录");
        return false;
      }
      var ids = new Array();
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认启用当前帐号？";
      if (ids.length > 1) {
        msg = "确认要启用已选中的（" + ids.length + "）个帐号？";
      }
      layer.confirm(msg, function (index) {
        fsoft.put(layui.cache['contentPath'] + '/sys-user/enable', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("启用成功！");
            active.doSearch();
          } else {
            fsoft.errorMsg("启用失败：" + rs.msg);
          }
        });
        layer.close(index);
      });
    },
    disable: function (data) { /*禁用*/
      if (data == undefined || data.length == 0) {
        fsoft.errorMsg("请至少选择1条要禁用的帐号记录");
        return false;
      }
      var ids = new Array();
      $.each(data, function (idx, item) {
        ids.push(item.id);
      });
      var msg = "确认要禁用当前帐号？";
      if (ids.length > 1) {
        msg = "确认要禁用已选中的（" + ids.length + "）个帐号？";
      }
      layer.confirm(msg, function (index) {
        fsoft.put(layui.cache['contentPath'] + '/sys-user/disable', ids, function (rs) {
          if (rs.code == 0) {
            fsoft.successMsg("禁用成功！");
            active.doSearch();
          } else {
            fsoft.errorMsg("禁用失败：" + rs.msg);
          }
        });
        layer.close(index);
      });
    }
  };

  /* 渲染左侧组织机构树 */
  var orgTree = eleTree.render({
    elem: '#' + treeId
    , method: 'post',
    url: layui.cache['contentPath'] + '/sys-org/findOrgTrees',
    request: { name: "title", key: "id" },
    highlightCurrent: true,
    expandOnClickNode: false,
    showLine: true
    , done: function (res) {
      if (res.code == 0) {
        var data = res.data;
        if (data != undefined && data.length > 0) {
          orgTree.expandNode(data[0].id);
        }
      }
      active.doSearch();
    }
  });

  //状态
  form.on('switch(status)', function (data) {
    var state = 1;
    var msg = "启用成功";
    var submitUrl = layui.cache['contentPath'] + "/sys-user/enable";
    if (data.elem.checked == false) {
      state = 0;
      msg = "禁用成功";
      submitUrl = layui.cache['contentPath'] + "/sys-user/disable";
    }
    fsoft.put(submitUrl, [this.value], function (rs) {
      if (rs.code == 0) {
        fsoft.successMsg(msg);
        return false;
      }
    });
  });
});